package controller;

import enums.SysInfoEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import pojo.paymentM;
import service.PayService;
import utils.ResultUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/Pay")
public class PayController {

    @Autowired
    PayService payService;

    @RequestMapping("/gotoget")
    public String gotogett(){
        return "/WEB-INF/view/paymentM/paymentM.jsp";
    }

    @RequestMapping("/get")
    @ResponseBody
    public List<paymentM> get(HttpServletRequest request, HttpServletResponse response){
        request.setAttribute("result",payService.getAll());
        System.out.println(payService.getAll());
        return payService.getAll();
    }


    @RequestMapping("/getOne")
    @ResponseBody
    public List<paymentM> getOne(@RequestParam(value = "stuId", required = false) Integer stuId, HttpServletRequest request, HttpServletResponse response){

        if (stuId == null){
            return payService.getAll();
        }

        List<paymentM> paymentMList = new ArrayList<>();
        paymentM paymentM = payService.getOne(stuId);
        paymentMList.add(paymentM);
        request.setAttribute("result",paymentMList);
//        System.out.println(paymentMList.get(0));
        return paymentMList;

    }

    @RequestMapping("/gotoedit")
    public String gotoedit(int stuId, HttpServletRequest request, HttpServletResponse response){

        request.setAttribute("stuId",stuId);
        return "/WEB-INF/view/paymentM/edit.jsp";
    }

    @RequestMapping("/edit")
    @ResponseBody
    public String edit(@RequestBody paymentM paymentM1){
        System.out.println(paymentM1);
        try{
            payService.update(paymentM1);
        }catch(Exception e){
            e.printStackTrace();
            return "失败";
        }
        return "成功";
    }

    @RequestMapping("/delete")
    public String del(String id){
        payService.del(id);
        return "/demo/Pay/gotoget";
    }

    @RequestMapping("/gotoadd")
    public String dotoadd(){
        return "/WEB-INF/view/paymentM/add.jsp";
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public String add(@Valid @RequestBody paymentM paymentM,HttpServletRequest request){
        System.out.println("1111");
        Integer result = 0;
        try{
           result =  payService.add(paymentM);
        }catch (Exception e){
            request.setAttribute("result","错误");
            e.printStackTrace();
            return ResultUtils.addResult(result);
        }

        return ResultUtils.addResult(result);
    }

    @RequestMapping(value = "/getByArray")
    @ResponseBody
    public List<paymentM> getByArray(@RequestParam(value = "currentPage",defaultValue = "1") Integer currentPage,
                                    @RequestParam(value = "size",defaultValue = "5") Integer size){
        List<paymentM> paymentMS =  payService.getPayByArray(currentPage, size);
        return paymentMS;
    }

    @RequestMapping(value = "/getNumber")
    @ResponseBody
    public Integer getNumber(){
        List<paymentM> paymentMS = payService.getAll();
        return paymentMS.size();
    }

}
